package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Projektarbete.Friend;
import Projektarbete.ContactTabListFiller;
import Projektarbete.User;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Zhoujie
 */
public class ContactPanel extends javax.swing.JPanel {
	ContactTabListFiller filler;
	private MainFrame mf;
	private ArrayList<Contact> contactList = new ArrayList<Contact>();
	private JPanel panel = new JPanel();
	
	public ContactPanel(MainFrame mf) {
        initComponents();
        this.mf = mf;
        filler = new ContactTabListFiller();
        addToTab(getJPanelWithContacts(mf));
        jScrollPane1.validate();
        setBackground(Color.WHITE);
    }
    
    public void addToTab(JPanel panel){
    	jScrollPane1.setViewportView(panel);
    }
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        setOpaque(false);
        setVerifyInputWhenFocusTarget(false);
        
        jButton1.setText("add friends");

        jButton2.setText("delete friends");
        jButton2.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		deleteButtonEvent();
        	}
        });
      

        jButton3.setText("chat");
        jButton3.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		chatButtonEvent();
        	}
        });
        
        jButton1.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		new AddContactFrame(mf).setVisible(true);
        	}
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2))
                .addGap(3, 3, 3)
                .addComponent(jButton3)
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
    }
    
    public ContactTabListFiller getFiller(){
    	return filler;
    }
    
    public JPanel getJPanelWithContacts(MainFrame mf) {
		ArrayList<User> userList = getFiller().getContactList(mf.getUser());
		
		
		Iterator<User> iterator = userList.iterator();
		panel.setLayout(new ColumnLayout(0, 0, 0, 0));
		panel.setBackground(Color.WHITE);
		
		while (iterator.hasNext()) {
			User nextFriend = iterator.next();
			Contact contact = new Contact(nextFriend);
			contactList.add(contact);
			panel.add(contact);
		}
		
		return panel;
	}
    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    public javax.swing.JScrollPane jScrollPane1;
    
   public void chatButtonEvent(){
		
	   	mf.sendChat(contactList);
   }
   
   
   
   public void deleteButtonEvent(){
		ArrayList<User> removeList = new ArrayList<User>();
		ArrayList<Contact> tempList = new ArrayList<Contact>();
		Iterator<Contact> iterator = contactList.iterator();
		while (iterator.hasNext()) {
			Contact nextContact = iterator.next();
			if (nextContact.isChecked()) {
				User removeUser = nextContact.getUser();
				removeList.add(removeUser);
				tempList.add(nextContact);
			}  
		}
				
		if(!removeList.isEmpty()){
			int confirm = JOptionPane.showConfirmDialog(panel, "Are you sure to remove the " +
					"checked contact(s) in your contact list?", "Warning! Remove Contact!", JOptionPane.YES_NO_OPTION);
			if (confirm == 0){
				filler.removeContact(mf.getUser(),removeList);
				
				
				for(int i = 0; i < tempList.size(); i++){
					panel.remove(tempList.get(i));
					tempList.get(i).setChecked(false);
				}
				panel.updateUI();
				
			}
		}else{
			JOptionPane.showMessageDialog(panel, "You have not checked any of " +
					"the contacts in your contact list, please try again!");
		}
	}
  
   public ArrayList<User> getUserList(){
	   ArrayList<User> userList = new ArrayList<User>();
	   for(int i = 0; i < contactList.size(); i++){
		   userList.add(contactList.get(i).getUser());
	   }
	   return userList;
	   
   }
   
   public void removeSingleContact(User user){
	   for(int i = 0; i < contactList.size(); i++){
		   if(contactList.get(i).getUser().getU_id() == user.getU_id()){
			   panel.remove(contactList.get(i));
			   contactList.remove(i);
		   }
	   }
	   panel.updateUI();
   }
   
   public User getUserForContact(Contact contact){
	   return contact.getUser();
   }

   public class ContactTabUpdater implements Runnable{
	   ContactTabListFiller filler = getFiller();
	   public void run() {	
		
		 
	   }
   }
   
   public void addSingleContact(User user){
	   Contact contact = new Contact(user);
	   contactList.add(contact);
	   panel.add(contact);
	   panel.updateUI();
   }
   
   public void updateOnlineStatus(User user){
	   for(int i = 0; i < contactList.size(); i++){
		   if(user.getU_id() == contactList.get(i).getUser().getU_id()){
			   contactList.get(i).setUser(user);
			   contactList.get(i).setStatus(user.isOnline());
			   contactList.get(i).updateUI();
			   panel.updateUI();
			   break;
		   }
	   }
   }
   
    
}
